const App = getApp()
Page({
  data: {
    cars: {},
    secondMenu: {}
  },
  onLoad () {
    let secondMenu = wx.getStorageSync('secondMenu')
    this.setData({secondMenu})
    this.onPullDownRefresh()
  },
  initData () {
    this.setData({
      cars: {
        items: [],
        total: '',
        params: {
          factoryId: '',
          page : 1,
          pageCount: 20
        }
      }
    })
  },
  getList (cb) {
    let cars = this.data.cars
    let params = cars.params
    let secondMenu = wx.getStorageSync('secondMenu')
    params.factoryId = secondMenu.id
    App.HttpService.getThird(params).then(res => {
      cb && cb()
      let data = res.data.data
      cars.items = [...cars.items, ...data.cars]
      if (data.cars.length > 0) {
        params.page = params.page + 1
        cars.total = data.counts
      }            
      this.setData({cars})
    }, e => {
      cb && cb()
    })
  },
  onPullDownRefresh () {
    wx.showNavigationBarLoading()
    this.initData()
    this.getList(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })
  },
  onReachBottom () {
    let cars = this.data.cars
    if (cars.items.length === cars.total) {
      return
    }
    this.getList()
  },
  toNext (e) {
    let thirdMenu = e.currentTarget.dataset
    wx.setStorageSync('thirdMenu', thirdMenu)
    wx.navigateTo({
      url: '/pages/car/add/index'
    })
  }
})